
// 根据话题id查询投票记录
import {NextRequest, NextResponse} from "next/server";
import {PrismaClient} from "@prisma/client"

// 创建prisma客户端对象
const prisma = new PrismaClient()

export async function GET(request: NextRequest) {
    try {
        const {searchParams, pathname} = new URL(request.url)
        const pathList = pathname.split("/")
        const userId = searchParams.get("userId") as string
        const topic = await prisma.topic.findFirst({
            where: {
                userId,
                id: parseInt(pathList[pathList.length - 1])
            },
            include: {
                options: true
            }
        })
        if (!topic) {
            return NextResponse.json({
                message: "用户没有投票"
            }, {status: 400})
        }
        return NextResponse.json({
            topic
        }, {status: 200})
    }catch (e) {
        // console.log(e)
        return NextResponse.json({
            message: "服务器发生异常"
        }, {status: 500})
    }finally {
        prisma.$disconnect()
    }
}